﻿package little.bunny;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
//import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import little.bunny.create_dh.CreateDaoHang;
/**
 * 内容显示页面
 * @author YaoRuiNing
 *
 */
public class ContentActivity extends Activity {
	private Button titleButton,backButton;
	private TextView conTextView;
	private ScrollView mScrollView;
	private Handler myHandler=new Handler();
	private String url;
	private String start,end ,moban;
	String a="         ";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.content_layout);
		
		mScrollView=(ScrollView)findViewById(R.id.scrollView1);
		mScrollView.setVerticalScrollBarEnabled(false);
		backButton=(Button)findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		
		
		Intent myIntent=getIntent();
		start=myIntent.getStringExtra("start");
		end=myIntent.getStringExtra("end");
		moban=myIntent.getStringExtra("moban");
		System.out.println("输出宽45546785135度"+moban);
		titleButton=(Button)findViewById(R.id.title_button_con);
		titleButton.setText(myIntent.getCharSequenceExtra("Title").toString());
		conTextView=(TextView)findViewById(R.id.content_textView_con);
		url=myIntent.getCharSequenceExtra("Con").toString();
		final ProgressDialog progressDialog = ProgressDialog.show(this, "请稍等",
				"数据加载中...", true);
		progressDialog.setOnKeyListener(new OnKeyListener() {
			
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK){
					progressDialog.dismiss();
				}
				return false;
			}
		});
		// 创建线程
		new Thread() {
			public void run() {
				Looper.prepare();
				try {
					//String str="    ";
					a=CreateDaoHang.getListContent(url,start,end,moban);
					
					a=a.replaceAll("\\s{2,}", "\n    ");
					a=a.replaceAll("<!--[\\s\\S]*?-->", "");
					a=a.replaceAll("/.*?/", "");
					//a=str+a;
					System.out.println("输出结果：547814835你妹："+a);
					if(a==null){
						progressDialog.dismiss();
						new AlertDialog.Builder(ContentActivity.this)
						.setTitle("网络信号异常")
						.setMessage("请查看网络连接，请稍后再试...")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										ContentActivity.this.finish();
									}
								}).show();
					}else{
						String str11 = a.replaceAll("table.MsoNormalTable[\\s\\S]*?\\}", "");
						a = str11.replaceAll("st1.*?[\\s\\S]*?\\}", "");
						myHandler.post(addValues);
					}
				} catch (Exception e) {
					// TODO: handle exception
					//System.out.println(e.getMessage());
					progressDialog.dismiss();
					System.out.println("ContentActivity获取服务器传递数据时候，报错。。。。。。。。。。");
					new AlertDialog.Builder(ContentActivity.this)
					.setTitle("网络信号异常")
					.setMessage("请查看网络连接，请稍后再试...")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									ContentActivity.this.finish();
								}
							}).show();
				}
				progressDialog.dismiss();// 隐藏进度条
				Looper.loop();
			}
		}.start();
	}
	
	final Runnable addValues = new Runnable() {

		public void run() {
			// TODO Auto-generated method stub
			//a 标签的作用就是 让标题 居中 
			conTextView.setText(a);
		}
		
	};
	
	//防止 手机横竖屏切换的时候 数据从新加载
	@Override 
	public void onConfigurationChanged(Configuration newConfig) { 
		                super.onConfigurationChanged(newConfig); 
		                if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
			                        // land do nothing is ok 
			                } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
				                        // port do nothing is ok 
				                } 
		        } 

}
